package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

import static com.sky.constant.OrdersConstant.*;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {

        Map map = new HashMap();
        Integer allOrders = orderMapper.statistics(map);
        map.put("status", ORDERS_STATUS_PENDING);
        Integer waitingOrders = orderMapper.statistics(map);
        map.put("status", ORDERS_STATUS_DELIVERY);
        Integer deliveredOrders = orderMapper.statistics(map);
        map.put("status", ORDERS_STATUS_CANCELED);
        Integer cancelledOrders = orderMapper.statistics(map);
        map.put("status", ORDERS_STATUS_COMPLETED);
        Integer completedOrders = orderMapper.statistics(map);

        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        orderOverViewVO.setAllOrders(allOrders);
        orderOverViewVO.setWaitingOrders(waitingOrders);
        orderOverViewVO.setDeliveredOrders(deliveredOrders);
        orderOverViewVO.setCancelledOrders(cancelledOrders);
        orderOverViewVO.setCompletedOrders(completedOrders);
        return orderOverViewVO;
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Integer discontinued = setmealMapper.overviewSetmeals(StatusConstant.DISABLE);
        Integer sold = setmealMapper.overviewSetmeals(StatusConstant.ENABLE);

        return SetmealOverViewVO
                .builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        Integer discontinued = dishMapper.overviewDishes(StatusConstant.DISABLE);
        Integer sold = dishMapper.overviewDishes(StatusConstant.ENABLE);

        return DishOverViewVO
                .builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO businessData(LocalDate begin, LocalDate end) {
//        LocalDate date = LocalDate.now();

        LocalDateTime min = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime max = LocalDateTime.of(end, LocalTime.MAX);

        Map map = new HashMap();
        map.put("beginTime", min);
        map.put("endTime", max);
        // 新增用户数
        Integer userCount = userMapper.getCount(map);
        userCount = userCount == null ? 0 : userCount;
        // 总订单数
        Integer allOrderCount = orderMapper.getOrdersCount(map);
        allOrderCount = allOrderCount == null ? 0 : allOrderCount;
        map.put("status", ORDERS_STATUS_COMPLETED);
        // 有效订单数
        Integer validOrderCount = orderMapper.getOrdersCount(map);
        validOrderCount = validOrderCount == null ? 0 : validOrderCount;
        // 订单完成率

        Double orderCompletionRate = allOrderCount == 0 ? 0.0 : Double.valueOf(validOrderCount) / Double.valueOf(allOrderCount);
        // 营业额
        Double amount = orderMapper.getAmount(map);
        amount = amount == null ? 0 : amount;
        // 平均客单价
        Double unitPrice = validOrderCount == 0 ? 0.0 : amount / Double.valueOf(validOrderCount);


        return BusinessDataVO
                .builder()
                .newUsers(userCount)
                .orderCompletionRate(orderCompletionRate)
                .validOrderCount(validOrderCount)
                .turnover(amount)
                .unitPrice(unitPrice)
                .build();
    }
}
